package com.luhuiguo.cms.server.service.restriction;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;

public class AttachmentFilter implements Serializable {

    private static final long serialVersionUID = 2403980400472950941L;

    public final static String ID = "id";
    public final static String ARTICLE_ID = "articleId";
    public final static String TYPE = "type";
    public final static String RANK = "rank";
    public final static String GRADE = "grade";

    private Long id;

    private Long articleId;

    private Integer type;

    private Integer rank;

    private Integer grade;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getArticleId() {
        return articleId;
    }

    public void setArticleId(Long articleId) {
        this.articleId = articleId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("AttachmentFilter [articleId=").append(articleId).append(", id=").append(id).append(", type=")
                .append(type).append(", rank=").append(rank).append(", grade=").append(grade).append("]");
        return builder.toString();
    }

    public String getWhere() {
        List<String> restrictions = new ArrayList<String>();
        if (null != id) {
            restrictions.add(ID + "=:" + ID);

        }
        if (null != articleId) {
            restrictions.add(ARTICLE_ID + "=:" + ARTICLE_ID);
        }
        if (null != type) {
            restrictions.add(TYPE + "=:" + TYPE);
        }
        if (null != rank) {
            restrictions.add(RANK + "=:" + RANK);
        }
        if (null != grade) {
            restrictions.add(GRADE + "=:" + GRADE);
        }
        return StringUtils.join(restrictions, " AND ");
    }

    public String toQueryString() {
        StringBuffer buf = new StringBuffer("SELECT FROM Attachment");
        String where = getWhere();
        if (StringUtils.isNotBlank(where)) {
            buf.append(" WHERE ").append(where);
        }
        buf.append(" ORDER BY lastModified desc");
        return buf.toString();
    }

    public String toCountString() {

        StringBuffer buf = new StringBuffer("SELECT COUNT() FROM Attachment");
        String where = getWhere();
        if (StringUtils.isNotBlank(where)) {
            buf.append(" WHERE ").append(where);
        }
        return buf.toString();
    }

    public void setParameters(Query q) {
        if (null != id) {
            q.setParameter(ID, id);
        }
        if (null != articleId) {
            q.setParameter(ARTICLE_ID, articleId);
        }
        if (null != type) {
            q.setParameter(TYPE, type);
        }
        if (null != rank) {
            q.setParameter(RANK, rank);
        }
        if (null != grade) {
            q.setParameter(GRADE, grade);
        }
    }

}
